/* PexifTool - $Revision$
 * Copyright (C) 2011 Petros Dolaschjan.  All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 *  ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
 *  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *	$Id$
 */
package de.pd.pjim2.controller.gui.dialog;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import net.miginfocom.swing.MigLayout;

import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import org.jdesktop.application.ApplicationActionMap;

import de.pd.pjim2.bean.PJSettings;
import de.pd.pjim2.util.FileUtil;
import de.pd.pjim2.util.SettingsUtil;

/**
 * @author petros
 *
 */
@SuppressWarnings("serial")
public class SettingsDialog extends JDialog {

	private final JPanel mcontentPanel = new JPanel();
	private JLabel mlabel;
	private JTextField tfPrefix;
	private JTextField tfSuffix;
	private JTextField tfFilePattern;
	private JTextField tfDirPattern;
	private JLabel lblPreview;

	/**
	 * Create the dialog.
	 */
	public SettingsDialog(JFrame pParent) {
		super(pParent, true);
		init();
		
		PJSettings settings = SettingsUtil.getSettings();
		loadSettings(settings);
	}

	/**
	 * 
	 */
	private void loadSettings(PJSettings pSettings) {
		tfPrefix.setText(pSettings.getImgPrefix());
		tfSuffix.setText(pSettings.getImgSuffix());
		tfFilePattern.setText(pSettings.getFilenamePattern());
		tfDirPattern.setText(pSettings.getDirnamePattern());
	}

	/**
	 * 
	 */
	private void init() {
		ApplicationActionMap actionMap = Application.getInstance().getContext()
				.getActionMap(this);
		setTitle("Settings");
		setBounds(100, 100, 550, 400);
		getContentPane().setLayout(new BorderLayout());
		mcontentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(mcontentPanel, BorderLayout.CENTER);
		mcontentPanel.setLayout(new MigLayout("", "[110px][146px,grow][110px][146px,grow]", "[26px][26px][26px][26px][150px,grow]"));
		
		mlabel = new JLabel("File Prefix");
		mcontentPanel.add(mlabel, "cell 0 0,alignx left");
		{
			tfPrefix = new JTextField();
			mcontentPanel.add(tfPrefix, "cell 1 0,growx");
			tfPrefix.setColumns(10);
		}
		{
			JLabel lblFileSuffix = new JLabel("File Suffix");
			mcontentPanel.add(lblFileSuffix, "cell 2 0,alignx left");
		}
		{
			tfSuffix = new JTextField();
			mcontentPanel.add(tfSuffix, "cell 3 0,growx");
			tfSuffix.setColumns(10);
		}
		{
			JLabel lblFilePattern = new JLabel("File Pattern");
			mcontentPanel.add(lblFilePattern, "cell 0 1,alignx left");
		}
		{
			tfFilePattern = new JTextField();
			mcontentPanel.add(tfFilePattern, "cell 1 1 3 1,growx");
			tfFilePattern.setColumns(10);
		}
		{
			JLabel lblDirPattern = new JLabel("Dir Pattern");
			mcontentPanel.add(lblDirPattern, "cell 0 2,alignx left");
		}
		{
			tfDirPattern = new JTextField();
			mcontentPanel.add(tfDirPattern, "cell 1 2 3 1,growx");
			tfDirPattern.setColumns(10);
		}
		{
			JButton btnPreview = new JButton(actionMap.get("previewAction"));
			lblPreview = new JLabel("");
			mcontentPanel.add(btnPreview, "cell 0 3, alignx left");
			mcontentPanel.add(lblPreview, "cell 1 3 3 1, growy, alignx left");
		}
		{
			String infoHTML = "<html><table summary='Customized Date and Time Format Samples' border='1'><caption>Customized Date and Time Formats</caption><tr><th id='h1'>Pattern</th><th id='h2'>Output</th></tr><tr><td headers='h1'>dd.MM.yy</td><td headers='h2'>30.06.09</td></tr><tr><td headers='h1'>yyyy.MM.dd G &#39;at&#39; hh:mm:ss z</td><td headers='h2'>2009.06.30 AD at 08:29:36 PDT</td></tr><tr><td headers='h1'>EEE, MMM d, &#39;&#39;yy</td><td headers='h2'>Tue, Jun 30, &#39;09</td></tr><tr><td headers='h1'>h:mm a</td><td headers='h2'>8:29 PM</td></tr><tr><td headers='h1'>H:mm</td><td headers='h2'>8:29</td></tr><tr><td headers='h1'>H:mm:ss:SSS</td><td headers='h2'>8:28:36:249</td></tr><tr><td headers='h1'>K:mm a,z</td><td headers='h2'>8:29 AM,PDT</td></tr><tr><td headers='h1'>yyyy.MMMMM.dd GGG hh:mm aaa</td><td headers='h2'>2009.June.30 AD 08:29 AM</td></tr></table></html>";
			JLabel lblInfo = new JLabel(infoHTML);
			JScrollPane sp = new JScrollPane(lblInfo);
			
			mcontentPanel.add(sp, "cell 0 4 4 1,grow, center");
		}
		
		
		
		
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton(actionMap.get("saveAction"));
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton(actionMap.get("cancelAction"));
				buttonPane.add(cancelButton);
			}
			{
				JButton defaultsButton = new JButton(actionMap.get("resetDefaultsAction"));
				defaultsButton.setText("Reset to Defaults");
				buttonPane.add(defaultsButton);
			}
		}
	}
	
	@Action
	public void cancelAction() {
		dispose();
	}
	
	@Action
	public void saveAction() {
		PJSettings newSettings = formToSettings();
		SettingsUtil.saveSettings(newSettings);
		dispose();
	}

	/**
	 * @return
	 */
	private PJSettings formToSettings() {
		PJSettings newSettings = new PJSettings();
		newSettings.setImgPrefix(tfPrefix.getText());
		newSettings.setImgSuffix(tfSuffix.getText());
		newSettings.setFilenamePattern(tfFilePattern.getText());
		newSettings.setDirnamePattern(tfDirPattern.getText());
		return newSettings;
	}
	
	@Action
	public void previewAction() {
		Date now = new Date();
		PJSettings newSettings = formToSettings();
		String dateToFilename = FileUtil.dateToFilename(now, newSettings, "jpg");
		String dateToFoldername = FileUtil.dateToFoldername(now, newSettings);
		String errMsg = "<b style='color: red'>Error parsing the pattern!</b>";
		if(dateToFilename == null) {
			dateToFilename = errMsg;
		}
		if(dateToFoldername == null) {
			dateToFoldername = errMsg;
		}
		lblPreview.setText("<html><b>File:</b> " + dateToFilename + "<br/><b>Dir</b>: " + dateToFoldername + "</html>");
	}
	
	@Action
	public void resetDefaultsAction() {
		loadSettings(SettingsUtil.getDefaultSettings());
	}
}
